<?php
//首页横幅控制器
class Controller_Admin_ConfigArray extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'config_array';
        parent::__construct($req, $res);

        $this->_key = $this->request->query('key');

        $this->_list_where = array(
        );

        if (! empty($this->_key))
            $this->_list_where[] = array('key', '=', $this->_key);
    }

	//默认首页
    public function action_index ()
    {
        $this->action_list();
    }

	//添加横幅
    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
           // $this->_form_data['name'] = $this->getNameByKey($this->_key);
        }

        if ($this->_method == 'POST')
        {
            $id = Form_Data::create($this->_form_type, $this->_form_data);
            $this->to_url('detail', $id);
        }
        else
        {
            $data['key']  = $this->_key;
           // $data['name'] = $this->getNameByKey($this->_key);
          
            $form_content =  $this->_form->set_data($data)->build_form();
            $this->template->set('form_content', $form_content);
            $this->template->set('form_include_edit_file', $this->_form->include_edit_file());
        }
 
    }

	//修改横幅
	//$nid int 横幅id
    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
        }

        parent::action_update($nid);
    }

	//横幅详细信息
	//$nid int 横幅id
    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 
        $this->_form_data = $data;

        parent::action_detail($nid);
    }

	//重构数据
    private function rebuild_post_data (& $post)
    {
    }

	// 列表数据改造
    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
        }
    }

	//菜单列表
    public function actions_by_list_row (& $row, & $menus)
    {
       // unset($menus[2]);
        unset($menus[0]);
    }

    protected function getNameByKey($key)
    {
        $name = DB::select('name')
            ->from('mod_config_array')
            ->where('key', '=', $key)
            ->limit(1)
            ->execute()
            ->get('name');

        return $name;
    }
	

}
